﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SQLite;

namespace ReAlchemy
{

    public partial class IngredientIn : Form
    {

        private bool listChanging = false;

        List<IngredientCLI> AllItems = new List<IngredientCLI>();

        public IngredientIn()
        {
            InitializeComponent();
        }

        private void IngredientIn_Shown(object sender, EventArgs e)
        {
            ResetList();
            // clbIngredients.Items.Clear();
            // Display database
            // db = new SQLiteConnection ("ingdata.db");
            

            /*
            listChanging = true;
            if (clbIngredients.CheckedItems.Count == clbIngredients.Items.Count)
                cbAll.CheckState = CheckState.Checked;
            else if (clbIngredients.CheckedItems.Count == 0)
                cbAll.CheckState = CheckState.Unchecked;
            else
                cbAll.CheckState = CheckState.Indeterminate;
            listChanging = false;
            */
        }

        public void ResetList() {
            AllItems.Clear();
            var table = MainDB.db.Table<Ingredient>();
            foreach (var s in table)
            {
                IngredientCLI Item = new IngredientCLI(s);
                AllItems.Add(Item);
                /*
                clbIngredients.Items.Add(Item);
                clbIngredients.SetItemChecked(clbIngredients.Items.Count-1, Item.Data.Checked == 1);
                 */
            }

            tboxListFilter.Text = "";
            applyFilter("");
        }

        private void IngredientIn_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.OK)
            {
                MainDB.db.BeginTransaction();

                if (GetCheckedCount() == AllItems.Count)
                {
                    MainDB.db.Execute("UPDATE Ingredient SET Checked = 1");
                }
                else
                {
                    MainDB.db.Execute("UPDATE Ingredient SET Checked = 0");

                    foreach (var ingr in AllItems.Where(itm => itm.Data.Checked == 1))
                    {
                        MainDB.db.Update(ingr.Data);
                    }
                }
                MainDB.db.Commit();
                // ((MainForm)Application.OpenForms["MainForm"]).updatedIngrList();
            }
            else { 
                // Nothing
            }
        }

        private void cbALL_CheckedChanged(object sender, EventArgs e)
        {
            if (listChanging) return; // Do not do anything if clbIngredients are changing
            
            if (cbAll.CheckState == CheckState.Checked)
            {
                /*
                foreach (IngredientCLI item in AllItems) {
                    item.Data.Checked = 1;
                }
                 //*/

                for (int index = 0; index < clbIngredients.Items.Count; index++)
                {
                    //AllItems.
                    clbIngredients.SetItemCheckState(index, CheckState.Checked);
                }
                //*/
            }
            else if (cbAll.CheckState == CheckState.Unchecked)
            {
                /*
                foreach (IngredientCLI item in AllItems)
                {
                    item.Data.Checked = 0;
                }
                 */

                for (int index = 0; index < clbIngredients.Items.Count; index++)
                {
                    clbIngredients.SetItemCheckState(index, CheckState.Unchecked);
                }
                 //*/ 
            }

            applyFilter(tboxListFilter.Text);
        }

        private int GetCheckedCount() {
            return AllItems.Where(item => item.Data.Checked == 1).Count();            
        }

        private void clbIngredients_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            listChanging = true;
            ((IngredientCLI)clbIngredients.Items[e.Index]).Data.Checked = e.NewValue == CheckState.Checked ? 1 : 0;
            /*
            int cc = GetCheckedCount();
            if (cc == 0) {
                cbAll.CheckState = CheckState.Unchecked;
            } else if (cc == AllItems.Count()) {
                cbAll.CheckState = CheckState.Checked;
            } else {
                cbAll.CheckState = CheckState.Indeterminate;
            }
             */ 
            
            if (clbIngredients.CheckedItems.Count == 1 && e.NewValue == CheckState.Unchecked)
            {
                // Empty:
                cbAll.CheckState = CheckState.Unchecked;
            }
            else if ((clbIngredients.CheckedItems.Count == clbIngredients.Items.Count - 1) && e.NewValue == CheckState.Checked)
            {
                // Full:
                cbAll.CheckState = CheckState.Checked;
            }
            else { 
                // Some On Some Off
                cbAll.CheckState = CheckState.Indeterminate;
            }
            
            listChanging = false;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void applyFilter(string sFilter) {
            clbIngredients.Items.Clear();
            sFilter = sFilter.ToLower();
            foreach (IngredientCLI item in AllItems) {
                if (sFilter == "" || item.Data.Name.ToLower().Contains(sFilter)) {
                    int item_index = clbIngredients.Items.Add(item);
                    clbIngredients.SetItemChecked(item_index, item.Data.Checked == 1);
                }
            }

        }

        private void tboxListFilter_TextChanged(object sender, EventArgs e)
        {
            applyFilter(tboxListFilter.Text);
        }

        private void btnClearFilter_Click(object sender, EventArgs e)
        {
            tboxListFilter.Text = "";
        }


    }

    public class IngredientCLI
    {
        public Ingredient Data;

        // define a text and
        // a tag value
        public IngredientCLI(Ingredient item) {
            this.Data = item;
        }

        // override ToString(); this
        // is what the checkbox control
        // displays as text
        public override string ToString()
        {
            return this.Data.Name;
        }
    }
}
